<template>
  <div class="spu">
    <el-card style="margin: 20px 0">
      <category-select :is-show="scene !== 0" @getCartgoryId="getCartgoryId"/>
    </el-card>

    <el-card>
      <!-- 三部分切换 -->
      <div v-show="scene === 0">
        <!-- 展示Spu列表的解构 -->
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addSpu">添加SPU</el-button>
        <el-table style="width: 100%" border :data="records">
          <el-table-column type="index" align="center" label="序号" width="80"/>
          <el-table-column prop="spuName" label="SPU名称" width="180"/>
          <el-table-column prop="description" label="SPU描述"/>
          <el-table-column prop="address" label="操作" width="300">
            <template slot-scope="{ row }">
              <hint-button type="success" icon="el-icon-plus" size="mini" title="添加sku" @click="addSku(row)"/>
              <hint-button type="warning" icon="el-icon-edit" size="mini" title="修改spu" @click="updateSpu(row)"/>
              <hint-button type="info" icon="el-icon-info" size="mini" title="查看当前spu全部sku列表"
                           @click="handler(row)"/>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="deleteSpu(row)">
                <hint-button type="danger" slot="reference" icon="el-icon-delete" size="mini" title="删除spu"/>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!--
        分页器
            @size-change="handleSizeChange"
          -->
        <el-pagination
            style="text-align: center"
            :current-page="page"
            :page-sizes="[3, 5, 10]"
            :page-size="limit"
            layout="prev, pager, next,jumper, ->, sizes, total"
            :total="total"
            @current-change="getSpuList"
            @size-change="handleSizeChange"
        />

      </div>
      <spu-form v-show="scene === 1" @changeScene="changeScene" ref="spu"></spu-form>
      <sku-form v-show="scene === 2" ref="sku" @changeScenes="changeScenes"></sku-form>
    </el-card>

    <el-dialog :title="`${spu.spuName}的sku列表`" :visible.sync="dialogTableVisible" :before-close="close">
      <el-table :data="skuList" style="width: 100%;" border v-loading="loading">
        <el-table-column property="skuName" label="名称" width="width"></el-table-column>
        <el-table-column property="price" label="价格" width="80"></el-table-column>
        <el-table-column property="weight" label="重量" width="80"></el-table-column>
        <el-table-column label="默认图片" width="width">
          <template slot-scope="{row}">
            <img :src="row.skuDefaultImg" alt="" style="width: 100px;height: 100px;">
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

  </div>
</template>

<script>

import SpuForm from '@/views/product/spu/SpuForm'
import SkuForm from '@/views/product/spu/SkuForm'

export default {
  name: 'Spu',
  components: {SpuForm, SkuForm},
  data() {
    return {
      category1Id: '',
      category2Id: '',
      category3Id: '',
      // 控制三级联动的可操作性
      isShow: true,
      page: 1,
      limit: 5,
      // spu列表
      records: [],
      // 分页器展示数据的总条数
      total: 0,
      // 0 展示SPU列表 1 添加or修改SPU  2 添加SKU
      scene: 0,
      dialogTableVisible: false,
      spu: {},
      skuList: [], // 存储sku列表的数据
      loading: true
    }
  },
  methods: {
    // 自定义事件的回调
    getCartgoryId({categoryId, level}) {
      if (level === 1) {
        this.category1Id = categoryId
        this.category2Id = ''
        this.category3Id = ''
      } else if (level === 2) {
        this.category2Id = categoryId
        this.category3Id = ''
      } else {
        // 代表三级分类的id有了
        this.category3Id = categoryId
        // 发请求获取Spu列表
        this.getSpuList()
      }
    },
    // 获取Spu列表的数据
    async getSpuList(pages = 1) {
      this.page = pages
      const {page, limit, category3Id} = this
      const result = await this.$API.spu.reqSpuList(page, limit, category3Id)
      // console.log(result)
      if (result.code === 200) {
        this.total = result.data.total
        this.records = result.data.records
      }
    },
    // 改变展示数据的条数
    handleSizeChange(limit) {
      this.limit = limit
      this.getSpuList()
    },
    // 添加spu按钮的回调
    addSpu() {
      this.scene = 1
      // 通知子组件SpuForm发请求
      this.$refs.spu.addSpuData(this.category3Id)
    },
    // 修改某一个spu
    updateSpu(row) {
      this.scene = 1
      // 获取子组件spuForm
      this.$refs.spu.initSpuData(row)
    },
    // 自定义事件的回调
    changeScene({scene, flag}) {
      // 切换场景
      this.scene = scene
      // flag区分保存按钮是添加还是修改
      if (flag === '修改') {
        this.getSpuList(this.page)
      } else {
        this.getSpuList()
      }
    },
    // 删除spu按钮的回调
    async deleteSpu(row) {
      const result = await this.$API.spu.reqDeleteSpu(row.id)
      if (result.code === 200) {
        this.$message({type: 'success', message: '删除成功'})
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1)
      }
    },
    // 添加Sku按钮的回调
    addSku(row) {
      // 切换场景
      this.scene = 2
      // 父组件调用子组件的方法， 子组件发请求
      this.$refs.sku.getData(this.category1Id, this.category2Id, row)
    },
    // skuform通知父组件切换场景
    changeScenes(scene) {
      this.scene = scene

    },
    // 查看sku按钮对应的回调
    async handler(spu) {
      this.dialogTableVisible = true
      // 保存spu信息
      this.spu = spu
      // 获取sku 列表的数据进行展示
      const result = await this.$API.spu.reqSkuList(spu.id);
      if (result.code === 200) {
        this.skuList = result.data
        //loading 隐藏
        this.loading = false
      }
    },
    // 关闭对话框的回调
    close(done) {
      // loading属性变为true
      this.loading = true
      // 清楚sku列表的数据
      this.skuList = []
      done()
    }
  }
}
</script>

<style scoped>

</style>
